package com.java.tutorials.spring.CallableStatement;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.CallableStatement;
import java.sql.SQLException;
/**
 * Created by xuzhike on 2016/3/13.
 * http://www.java2s.com/Code/Java/Spring/CallableStatementCallbackAndProcedure.htm
 * CallableStatement Callback And Procedure
 */
public class CallableStatementTest {
    public static void main(String args[]) throws Exception {
        ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", CallableStatementTest.class);
        DataSource dataSource = (DataSource) ac.getBean("dataSource");
        // DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        jdbcTemplate.execute("{call p_proc(42)}", new CallableStatementCallback() {
            public Object doInCallableStatement(CallableStatement callableStatement) throws SQLException,
                    DataAccessException {
                callableStatement.execute();
                return null;
            }
        });
    }
}
